package com.jly.edu.controller;


import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jly.edu.common.dto.SysDictTypeDto;
import com.jly.edu.common.lang.Const;
import com.jly.edu.common.lang.Result;
import com.jly.edu.common.validated.SaveGroup;
import com.jly.edu.common.validated.UpdateGroup;
import com.jly.edu.entity.SysDictItem;
import com.jly.edu.entity.SysDictType;
import com.jly.edu.service.SysDictItemService;
import com.jly.edu.service.SysDictTypeService;
import com.jly.edu.utils.ConverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者：纪联友
 * @since 2021-10-26
 */
@RestController
@RequestMapping("/sys-dict-type")
public class SysDictTypeController extends BaseController {


    @Autowired
    private SysDictTypeService sysDictTypeService;
    @Autowired
    private SysDictItemService sysDictItemService;

    /**
     * 获取字典类型
     * @param code
     * @param type
     * @return
     */
    @PreAuthorize("hasAnyAuthority('sys:dict:list')")
    @GetMapping("/list")
    public Result getSysDictTypeList(String code,Integer type){
        QueryWrapper<SysDictType> queryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotBlank(code)){
            queryWrapper.like("code",code);
        }
        if(type != null){
            queryWrapper.eq("type",type);
        }
        Page<SysDictType> dictTypePage= sysDictTypeService.page(getPage(), queryWrapper);
        return Result.success(dictTypePage);
    }

    @PreAuthorize("hasAnyAuthority('sys:dict:list')")
    @GetMapping("/info/{id}")
    public Result getSysDictTypeById(@PathVariable(name = "id") Long id){
        Assert.notNull(id,"字典类型id不能为空!");
        SysDictType sysDictType = sysDictTypeService.getById(id);
        return Result.success(sysDictType);
    }


    @PreAuthorize("hasAnyAuthority('sys:dict:save')")
    @PostMapping("/save")
    public Result saveDictType(@Validated(SaveGroup.class) @RequestBody SysDictTypeDto sysDictTypeDto){
        SysDictType sysDictType = sysDictTypeService.getOne(new QueryWrapper<SysDictType>().eq("code", sysDictTypeDto.getCode()));
        if(sysDictType != null){
            return Result.fail("类型已经存在");
        }
        SysDictType finalDictType = new SysDictType();
        finalDictType.setCreateTime(LocalDateTime.now());
        finalDictType.setStatus(Const.STATUS_ON);
        ConverUtil.convert(sysDictTypeDto,finalDictType);
        sysDictTypeService.save(finalDictType);
        return Result.success(null);
    }

    @PreAuthorize("hasAnyAuthority('sys:dict:update')")
    @PostMapping("/update")
    public Result updateDictType(@Validated(UpdateGroup.class) @RequestBody SysDictTypeDto sysDictTypeDto){
        SysDictType finalDictType = new SysDictType();
        ConverUtil.convert(sysDictTypeDto,finalDictType);
        sysDictTypeService.updateById(finalDictType);
        return Result.success(null);
    }


    @Transactional
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('sys:dict:delete')")
    public Result deleteDictType(@PathVariable(name = "id") Long id) {
        SysDictType sysDictType = sysDictTypeService.getById(id);
        if(Const.STATUS_ON.equals(sysDictType.getStatus())){
            return Result.fail("字典类型启用中，无法删除");
        }
        sysDictTypeService.removeById(id);

        sysDictItemService.getBaseMapper().delete(new QueryWrapper<SysDictItem>().eq("type_id",id));

        return Result.success(null);
    }


}
